import java.util.LinkedList;
import java.util.Queue;

//一个普通队列无法实现栈
//用两个队列实现栈
public class MyStack {
    private Queue<Integer> qu1;
    private Queue<Integer> qu2;

    public MyStack() {
        this.qu1 = new LinkedList<>();
        this.qu2 = new LinkedList<>();
    }

    //入到不为空的队列中
    public void push(int x) {
        if(qu1.isEmpty() ) {
            if(qu2.isEmpty()){
                qu1.offer(x);
            }else {
                qu2.offer(x);
            }
        }else {
            qu1.offer(x);
        }
    }

    public int pop() {
        if(empty()){
            return -1;//代表栈为空
        }
       if(!qu1.isEmpty()){
           int size = qu1.size();
           for (int i = 0; i < size-1; i++) {
              int tmp = qu1.poll();
              qu2.offer(tmp);
           }
           return qu1.poll();
       }else {
           int size = qu2.size();
           for (int i = 0; i <size-1; i++) {
               int tmp = qu2.poll();
               qu1.offer(tmp);
           }
           return qu2.poll();
       }
    }

    //peek
    public int top() {
        int tmp = 0;
        if(empty()){
            return -1;//代表栈为空
        }
        if(!qu1.isEmpty()){
            int size = qu1.size();
            for (int i = 0; i < size; i++) {
                 tmp = qu1.poll();
                qu2.offer(tmp);
            }
            return tmp;
        }else {
            int size = qu2.size();
            for (int i = 0; i <size; i++) {
                 tmp = qu2.poll();
                qu1.offer(tmp);
            }
            return tmp;
        }
    }



    //两个队列都为空时代表栈为空
    public boolean empty() {
        return qu1.isEmpty() && qu2.isEmpty();
    }
}
